import {BasicColumn, FormSchema} from '@/components/table';
import {h} from 'vue';
import {Tag} from 'ant-design-vue';
import {Icon} from '@components/common';

export const columns: BasicColumn[] = [
  {
    title: '菜单名称',
    dataIndex: 'menuName',
    width: 200,
    align: 'left',
  },
  {
    title: '图标',
    dataIndex: 'icon',
    width: 50,
    customRender: ({record}: any) => {
      return h(Icon, {icon: record.icon});
    },
  },
  {
    title: '权限标识',
    dataIndex: 'menuCode',
    width: 180,
  },
  {
    title: '路径',
    dataIndex: 'path',
    width: 180,
  },
  {
    title: '组件',
    dataIndex: 'menuLayout',
    width: 200,
  },
  {
    title: '排序',
    dataIndex: 'priority',
    width: 50,
  },
  {
    title: '状态',
    dataIndex: 'status',
    width: 80,
    customRender: ({record}:any) => {
      const status = record.status;
      const enable = status === 1;
      const color = enable ? 'green' : 'red';
      const text = enable ? '启用' : '停用';
      return h(Tag, {color: color}, () => text);
    },
  },
  {
    title: '创建时间',
    dataIndex: 'createTime',
    width: 180,
  },
];

const isDir = (type: string) => type === '0';
const isMenu = (type: string) => type === '1';
const isButton = (type: string) => type === '2';

export const searchFormSchema: FormSchema[] = [
  {
    field: 'menuName',
    label: '菜单名称',
    component: 'Input',
    colProps: {span: 8},
  },
  {
    field: 'status',
    label: '状态',
    component: 'Select',
    componentProps: {
      options: [
        {label: '启用', value: '0'},
        {label: '停用', value: '1'},
      ],
    },
    colProps: {span: 8},
  },
];

export const formSchema: FormSchema[] = [
  {
    field: 'menuId',
    label: '菜单Id',
    component: 'Input',
    required: false,
    show: false,
  },
  {
    field: 'type',
    label: '菜单类型',
    component: 'RadioButtonGroup',
    defaultValue: '0',
    componentProps: {
      options: [
        {label: '目录', value: '0'},
        {label: '菜单', value: '1'}
      ],
    },
  },
  {
    field: 'menuName',
    label: '菜单名称',
    component: 'Input',
    required: true,
  },
  {
    field: 'menuCode',
    label: '菜单编码',
    component: 'Input',
    required: true,
  },
  {
    field: 'parentId',
    label: '上级菜单',
    component: 'TreeSelect',
    componentProps: {
      fieldNames: {
        label: 'menuName',
        key: 'parentId',
        value: 'menuId',
      },
      getPopupContainer: () => document.body,
    },
  },

  {
    field: 'priority',
    label: '排序',
    component: 'InputNumber',
    required: true,
  },
  {
    field: 'icon',
    label: '图标',
    component: 'IconPicker',
    required: true,
    ifShow: ({values}) => !isButton(values.type),
  },

  {
    field: 'path',
    label: '路由地址',
    component: 'Input',
    required: true,
    ifShow: ({values}) => !isButton(values.type),
  },
  {
    field: 'menuLayout',
    label: '组件路径',
    component: 'Input',
    ifShow: ({values}) => isMenu(values.type),
  },
  {
    field: 'menuCode',
    label: '权限标识',
    component: 'Input',
    ifShow: ({values}) => !isDir(values.type),
  },
  {
    field: 'status',
    label: '状态',
    component: 'RadioButtonGroup',
    defaultValue: '1',
    componentProps: {
      options: [
        {label: '启用', value: '1'},
        {label: '禁用', value: '0'},
      ],
    },
  },
  {
    field: 'isExt',
    label: '是否外链',
    component: 'RadioButtonGroup',
    defaultValue: '0',
    componentProps: {
      options: [
        {label: '否', value: '0'},
        {label: '是', value: '1'},
      ],
    },
    ifShow: ({values}) => !isButton(values.type),
  },

  {
    field: 'keepAlive',
    label: '点击刷新',
    component: 'RadioButtonGroup',
    defaultValue: 0,
    componentProps: {
      options: [
        {label: '否', value: 0},
        {label: '是', value: 1},
      ],
    },
    ifShow: ({values}) => isMenu(values.type),
  },
  {
    field: 'show',
    label: '是否显示',
    component: 'RadioButtonGroup',
    defaultValue: '0',
    componentProps: {
      options: [
        {label: '是', value: '0'},
        {label: '否', value: '1'},
      ],
    },
    ifShow: ({values}) => !isButton(values.type),
  },
];
